package vip.xiaonuo.bi.core.datasource.util;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import vip.xiaonuo.bi.modular.dataset.entity.DataColumns;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 数据库工具类
 * 主要是关系型数据库
 *   @author 顾育司
 */
public class DButils {

    /**
     * 对关系型数据集获取表的属性列
     * 查询表格数据永为空
     * @param tableName
     * @return
     */
    public static String getQueryColumnSql(String tableName) {
        return   String.format("select * from `%s` where 1=2;", tableName);
    }

    /**
     * 对关系型数据集获取表的属性列
     * 通过执行sql 获取
     * @param sql
     * @return
     */
    public static String getQueryColumnBySql(String sql) {
        return   String.format("select * from (%s) t   where 1=2", sql);
    }

    /**
     * 对关系型数据集获取表的总条数
     *
     * @param sql
     * @return
     */
    public static String getQueryCountBySql(String sql) {
        return   String.format("select count(1) as data_count from (%s) t ", sql);
    }

    /**
     * 对关系型数据集获取表的总条数
     *
     * @param table
     * @return
     */
    public static String getQueryCountByTable(String table) {
        return   String.format("select count(1) as data_count from %s ", table);
    }

    /**
     * 对关系型数据集获取表的总条数
     *
     * @param table
     * @return
     */
    public static String getQueryDataSizeByTable(String table) {
        return   String.format("select DATA_LENGTH as data_size from information_schema.tables where TABLE_NAME='%s'", table);
    }

    public static String getQueryTableSql(String tableName) {
        return   String.format("select * from `%s`", tableName);
    }


    public static String getTableSizeSql(String tableName) {
        return   String.format(" select DATA_LENGTH as data_size  from information_schema.tables where TABLE_NAME = '%s'", tableName);
    }


    public static String getTablePreViewSql(String tableName,Integer limit ) {
        String sql = String.format(" select * from %s", tableName);
        if(ObjectUtils.isNotEmpty(limit)){
            sql = sql+ " limit " + limit;
        }
        return   sql;
    }

    public static String getTablePreViewSqlByCols(List<DataColumns> dataColumns, String tableName, Integer limit ) {
        List<String> collect = dataColumns.stream().map(item -> (ObjectUtils.isEmpty(item.getCalculate()) ? item.getTableCol() : item.getCalculate()) + " as " + item.getName()).collect(Collectors.toList());
        // 拼接
        String select = String.join(",", collect);
        // from
        String sql = String.format(" select %s from %s",select, tableName);
        // 条数
        if(ObjectUtils.isNotEmpty(limit)){
            sql = sql+ " limit " + limit;
        }
        return   sql;
    }

    // 获取总条数语句
    public static String getCountSql(String querySql) throws Exception {
//        String tableName = "";
        String queryFields = "";

        // 使用正则表达式提取表名和查询字段
        Pattern pattern = Pattern.compile("SELECT\\s+(.*?)\\s+FROM\\s+(\\w+)", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(querySql);

        if (matcher.find()) {
            queryFields = matcher.group(1);
            // 获取表名
//            tableName = matcher.group(2);
        } else {
            throw new Exception("Invalid query");
        }
        // 将查询字段替换为COUNT(1)
        String countQuery = querySql.replace(queryFields, "COUNT(1)");
        return countQuery; // 返回总条数，这里暂时返回0，实际实现中需要根据具体情况进行处理
    }

}
